package 抽象数据类型;

/**
 * @description:
 * @author: ywk
 * @date: 2021-08-14
 */
public class EdgeWeightedGraph {
    private final int V;
    private int E;
    private Bag<DirectedEdge>[] adj;

    public EdgeWeightedGraph(int V) {
        this.V = V;
        this.E = 0;
        adj = new Bag[V];
        for (int v = 0; v < V; v++) {
            adj[v] = new Bag<>();
        }
    }

    public int V() {
        return this.V;
    }

    public int E() {
        return this.E;
    }

    public void addGraph(int[] arr1, int[] arr2, double[] distance) {
        for (int i = 0; i < arr1.length; i++) {
            DirectedEdge edge = new DirectedEdge(arr1[i], arr2[i], distance[i]);
            addEdge(edge);
        }
    }

    public void addEdge(DirectedEdge edge) {
        adj[edge.from()].add(edge);
        E++;
    }

    public Iterable<DirectedEdge> adj(int v) {
        return adj[v];
    }

    public Iterable<Edge> edges() {
        Bag<Edge> b = new Bag<>();
        for (int v = 0; v < this.V; v++) {
            for (Edge edge : adj[v]) {
                if (edge.other(v) > v) {
                    b.add(edge);
                }
            }
        }
        return b;
    }

}
